package com.loto.insurance.mis.web.controller.ajax;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.loto.insurance.mis.web.authority.INSAuthority;
import com.loto.insurance.mis.web.entity.ajax.BaseJson;
import com.loto.insurance.mis.web.entity.ajax.GridJson;
import com.loto.insurance.mis.web.entity.ajax.OemInfo;
import com.loto.insurance.mis.web.enumeration.ControllerPropertyEnum;
import com.loto.insurance.mis.web.enumeration.MenuEnum;
import com.loto.insurance.mis.web.enumeration.ResultTypeEnum;
import com.loto.insurance.mis.web.service.CustInfoService;
import com.loto.insurance.mis.web.service.OemService;
import com.loto.insurance.mis.web.service.ProductInfoService;
import com.loto.insurance.mis.web.service.UserInfoService;
import com.loto.insurance.mis.web.utils.Code;
import com.loto.insurance.mis.web.entity.CustInfo;
import com.loto.insurance.mis.web.entity.Pager;
import com.loto.utils.DateUtils;

@Controller
@SessionAttributes("ins_user")
public class OemJsonController {
  @Autowired
  UserInfoService userInfoService;

  @Autowired
  OemService oemService;

  @Autowired
  CustInfoService custInfoService;

  @Autowired
  ProductInfoService productInfoService;

  @INSAuthority(authorityMenu = MenuEnum.OEM_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/oem/list.json", method = RequestMethod.POST)
  public ModelAndView oemList(
	  @RequestParam(value = "oemName", required = false) String oemName,
	  @RequestParam(value = "oemBegin", required = false) String oemBegin,
	  @RequestParam(value = "oemEnd", required = false) String oemEnd,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  @RequestParam(value = "rows", required = false, defaultValue = "20") int rows) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (oemName != null && !oemName.isEmpty())
	  params.put("oemName", oemName);
	if (oemBegin != null && oemEnd != null
	    && DateUtils.isDate(oemBegin, "yyyy-MM-dd")
	    && DateUtils.isDate(oemEnd, "yyyy-MM-dd")) {
	  params.put("oemBegin", oemBegin);
	  params.put("oemEnd", oemEnd);
	}

	Pager pager = new Pager();
	pager.setPageSize(rows);

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	List<com.loto.insurance.mis.web.entity.OemInfo> oemList = oemService
	    .findAllOem(params);

	List<OemInfo> list = new ArrayList<OemInfo>();
	for (com.loto.insurance.mis.web.entity.OemInfo oem : oemList) {
	  OemInfo o = new OemInfo();
	  o.setCustName(oem.getCustName());
	  o.setCustUsefulFee(oem.getCustUsefulFee());
	  o.setOemCreatedOn(DateUtils.formatShortDateTime(oem.getOemCreatedOn()));
	  o.setOemDomain(oem.getOemDomain());
	  o.setOemId(oem.getOemId());
	  o.setOemName(oem.getOemName());
	  o.setOemServiceTel(oem.getOemServiceTel());
	  o.setOemValidTime(DateUtils.formatDate(oem.getOemBeginDate()) + "  至  "
		  + DateUtils.formatDate(oem.getOemEndDate()));

	  list.add(o);
	}

	pager.setRowCount(oemService.findAllOemCount(params));

	ModelAndView mav = new ModelAndView();

	GridJson<OemInfo> gridJson = new GridJson<OemInfo>();
	gridJson.setTotal(pager.getRowCount());
	gridJson.setRows(list);

	mav.addObject("result", gridJson);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.OEM_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/oem/add.json", method = RequestMethod.POST)
  public ModelAndView oemAdd(
	  @RequestParam(value = "name") String name,
	  @RequestParam(value = "custName") String custName,
	  @RequestParam(value = "begin") String begin,
	  @RequestParam(value = "end") String end,
	  @RequestParam(value = "domain") String domain,
	  @RequestParam(value = "icp") String icp,
	  @RequestParam(value = "contact") String contact,
	  @RequestParam(value = "tel") String tel,
	  @RequestParam(value = "email") String email,
	  @RequestParam(value = "ip") String ip,
	  @RequestParam(value = "service") String service,
	  @RequestParam(value = "ispayee") int ispayee,
	  @RequestParam(value = "alipayemail") String alipayemail,
	  @RequestParam(value = "alipaypartner") String alipaypartner,
	  @RequestParam(value = "alipaysecret") String alipaysecret,
	  @RequestParam(value = "alipayrate", required = false, defaultValue = "0.012") BigDecimal alipayrate) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);

	CustInfo custInfo = custInfoService.findByName(custName);
	if (custInfo != null) {
	  List<com.loto.insurance.mis.web.entity.OemInfo> oemList = oemService
		  .findOemByCustId(custInfo.getCustId());
	  if (oemList.size() == 0) {
		com.loto.insurance.mis.web.entity.OemInfo oemInfo = new com.loto.insurance.mis.web.entity.OemInfo();
		oemInfo.setOemId(UUID.randomUUID().toString().replace("-", ""));
		oemInfo.setOemAcc(Code.generateCode(10));
		oemInfo.setOemAlipayEmail(alipayemail);
		oemInfo.setOemAlipayPartner(alipaypartner);
		oemInfo.setOemAlipayRate(alipayrate);
		oemInfo.setOemAlipaySecret(alipaysecret);
		oemInfo.setOemBeginDate(DateUtils.parseDate(begin));
		oemInfo.setOemContactEmail(email);
		oemInfo.setOemContactName(contact);
		oemInfo.setOemContactTel(tel);
		oemInfo.setOemCustFid(custInfo.getCustId());
		oemInfo.setOemDomain(domain);
		oemInfo.setOemEndDate(DateUtils.parseDate(end));
		oemInfo.setOemIcp(icp);
		oemInfo.setOemIp(ip);
		oemInfo.setOemIsPayeeAlipay(ispayee);
		oemInfo.setOemName(name);
		oemInfo.setOemPin(UUID.randomUUID().toString().replace("-", ""));
		oemInfo.setOemServiceTel(service);

		if (oemService.addOem(oemInfo)) {
		  productInfoService.notifyProductInfo(oemInfo.getOemId());
		}

		baseJson.setOk(true);
	  } else {
		baseJson.setMsg(ControllerPropertyEnum.OEM_CUST_EXISTED.toString());
	  }

	} else {
	  baseJson.setMsg(ControllerPropertyEnum.CUST_NOT_EXISTED.toString());
	}
	ModelAndView mav = new ModelAndView();
	mav.addObject("result", baseJson);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.OEM_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/oem/modify.json", method = RequestMethod.POST)
  public ModelAndView oemModify(
	  @RequestParam(value = "id") String id,
	  @RequestParam(value = "name") String name,
	  @RequestParam(value = "custName") String custName,
	  @RequestParam(value = "begin") String begin,
	  @RequestParam(value = "end") String end,
	  @RequestParam(value = "domain") String domain,
	  @RequestParam(value = "icp") String icp,
	  @RequestParam(value = "contact") String contact,
	  @RequestParam(value = "tel") String tel,
	  @RequestParam(value = "email") String email,
	  @RequestParam(value = "ip") String ip,
	  @RequestParam(value = "service") String service,
	  @RequestParam(value = "ispayee") int ispayee,
	  @RequestParam(value = "alipayemail") String alipayemail,
	  @RequestParam(value = "alipaypartner") String alipaypartner,
	  @RequestParam(value = "alipaysecret") String alipaysecret,
	  @RequestParam(value = "alipayrate", required = false, defaultValue = "0.012") BigDecimal alipayrate) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);

	CustInfo custInfo = custInfoService.findByName(custName);
	if (custInfo != null) {
	  List<com.loto.insurance.mis.web.entity.OemInfo> oemList = oemService
		  .findOemByCustId(custInfo.getCustId());
	  if (oemList.size() == 0
		  || (oemList.size() == 1
		      && oemList.get(0).getOemCustFid() == custInfo.getCustId() && oemList
		      .get(0).getOemId().equals(id))) {
		com.loto.insurance.mis.web.entity.OemInfo oemInfo = new com.loto.insurance.mis.web.entity.OemInfo();
		oemInfo.setOemId(id);
		oemInfo.setOemAlipayEmail(alipayemail);
		oemInfo.setOemAlipayPartner(alipaypartner);
		oemInfo.setOemAlipayRate(alipayrate);
		oemInfo.setOemAlipaySecret(alipaysecret);
		oemInfo.setOemBeginDate(DateUtils.parseDate(begin));
		oemInfo.setOemContactEmail(email);
		oemInfo.setOemContactName(contact);
		oemInfo.setOemContactTel(tel);
		oemInfo.setOemCustFid(custInfo.getCustId());
		oemInfo.setOemDomain(domain);
		oemInfo.setOemEndDate(DateUtils.parseDate(end));
		oemInfo.setOemIcp(icp);
		oemInfo.setOemIp(ip);
		oemInfo.setOemIsPayeeAlipay(ispayee);
		oemInfo.setOemName(name);
		oemInfo.setOemServiceTel(service);

		oemService.updateOem(oemInfo);

		baseJson.setOk(true);
	  } else {
		baseJson.setMsg(ControllerPropertyEnum.OEM_CUST_EXISTED.toString());
	  }
	} else {
	  baseJson.setMsg(ControllerPropertyEnum.CUST_NOT_EXISTED.toString());
	}
	ModelAndView mav = new ModelAndView();
	mav.addObject("result", baseJson);
	return mav;
  }
}
